//
//  RootTableViewController.m
//  MyFunctionDemo
//
//  Created by Ting Han on 2017/9/21.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "RootTableViewController.h"
#import "RootItemCell.h"
#import "UIView+SetRect.h"
#import "Item.h"
#import "RootViewController.h"
#import "DefaultNotificationCenter.h"
#import "GCDHeader.h"
#import "IndexPathRange.h"

#import "TransViewController.h"

@interface RootTableViewController ()<UITableViewDelegate,UITableViewDataSource,DefaultNotificationCenterDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *items;
@property (nonatomic,strong) DefaultNotificationCenter *notificationCenter;

@property (nonatomic,assign) BOOL isLoadData;
@end

@implementation RootTableViewController

static NSString *rootListCellId = @"RootListCellId";

- (void)viewDidLoad {
    [super viewDidLoad];
    //push转场动画
    self.navigationController.delegate = self;
    
    [self setupNotificationCenter];
    [self configTableView];
    [self initDataSource];
    
}
- (void)setupNotificationCenter{
    self.notificationCenter = [DefaultNotificationCenter defaultNotificationCenterWithDelegate:self addNotificationNames:^(NSMutableArray<NSString *> *names) {
        [names addObject:noti_showHomePageTableView];
    }];
}
- (void)defaultNotificationCenter:(DefaultNotificationCenter *)notification name:(NSString *)name object:(id)object{
    if ([name isEqualToString:noti_showHomePageTableView]) {
        self.isLoadData = YES;
        [GCDQueue executeInMainQueue:^{
            
            [self.tableView insertRowsAtIndexPaths:   MakeIndexPathRanges(@[MakeIndexPathRange(0, 0, self.items.count)]) withRowAnimation:UITableViewRowAnimationFade];
        }];
    }
}
- (void)setupDataSource{
    self.title = @"Animation";
    self.isLoadData = NO;
}
- (void)setupSubViews{
    UILabel *headlinelabel      = [UILabel new];
    headlinelabel.font          = [UIFont fontWithName:@"Heiti SC" size:20.f];
    headlinelabel.textAlignment = NSTextAlignmentCenter;
    headlinelabel.textColor     = [UIColor colorWithRed:0.329  green:0.329  blue:0.329 alpha:1];
    headlinelabel.text          = self.title;
    [headlinelabel sizeToFit];
    CGPoint point = CGPointMake(self.titleView.middlePoint.x, self.titleView.middlePoint.y+10);
    headlinelabel.center = point;
    
    // Line.
    UIView *line         = [[UIView alloc] initWithFrame:CGRectMake(0, 63.5, self.view.width, 0.5f)];
    line.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.25f];
    [self.titleView addSubview:line];
    [self.titleView addSubview:headlinelabel];
}

- (void)configTableView{
    self.tableView                = [[UITableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
    self.tableView.delegate       = self;
    self.tableView.dataSource     = self;
    self.tableView.rowHeight      = 50.f;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableView];
    [RootItemCell registerToTableView:self.tableView reuseIdentifier:rootListCellId];
}
- (void)initDataSource{
    NSArray *array = @[
                       [Item itemWithName:@"转场动画" data:[TransViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]],
                       [Item itemWithName:@"第一个" data:[RootViewController class]]
                       ];
    self.items = [NSMutableArray array];
    
    for (int i = 0; i < array.count; i++) {
        
        Item *item = array[i];
        item.index = i + 1;
        [item createAttributedString];
        [self.items addObject:[CellDataAdapter cellDataAdapterWithReuseIdentifier:rootListCellId data:item cellHeight:0 cellWidth:0 cellType:0]];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    RootItemCell *cell = (RootItemCell *)[tableView dequeueReusableCellAndLoadDataWithAdapter:self.items[indexPath.row] indexPath:indexPath];
    cell.viewController = self;
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.isLoadData? self.items.count : 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     [(RootItemCell *)[tableView cellForRowAtIndexPath:indexPath] selectedEvent];
}



@end
